package com.adms.util;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

import javax.imageio.ImageIO;

import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.springframework.web.multipart.MultipartFile;

public class FileUtil {

	public static String RES_SERVER;
	public static String IMG_PATH;
	public static String PDF_PATH;
	public static String VIDEO_PATH;

	public static final String PDF_TYPE = ".pdf";
	public static final List<String> IMG_TYPE = Arrays.asList(".jpg", ".gif", ".bmp", ".png", ".jpeg", ".ico");
	public static final List<String> VIDEO_TYPE = Arrays.asList(".mp4", ".rmvb", ".avi", ".wmv", ".3gp", ".mkv",
			".flv");

	private static final DiskFileItemFactory FACTORY = new DiskFileItemFactory();

	static {
		FACTORY.setSizeThreshold(10 * 1024 * 1024);
	}

	public static String uploadImg(MultipartFile file) {
		String type = getFileType(file);
		if (!IMG_TYPE.contains(type)) {
			throw new RuntimeException("图片格式不正确");
		}
		String path;
		try {
			path = IMG_PATH + UUID.randomUUID() + type;
			file.transferTo(new File(path));
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException("上传失败");
		}
		return path;
	}

	public static String uploadPdf(MultipartFile file) {
		String type = getFileType(file);
		if (!PDF_TYPE.contains(type)) {
			throw new RuntimeException("pdf格式不正确");
		}
		String path;
		try {
			path = PDF_PATH + UUID.randomUUID() + type;
			file.transferTo(new File(path));
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException("上传失败");
		}
		return path;
	}

	public static String uploadVideo(MultipartFile file) {
		String type = getFileType(file);
		if (!VIDEO_TYPE.contains(type)) {
			throw new RuntimeException("视频格式不正确");
		}
		String path;
		try {
			path = VIDEO_PATH + UUID.randomUUID() + type;
			file.transferTo(new File(path));
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException("上传失败");
		}
		return path;
	}

	private static String getFileType(MultipartFile file) {
		return file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")).toLowerCase();
	}

	// 裁切图片
	public static ByteArrayOutputStream cutImage(File imgFile, Integer w, Integer h) throws IOException {
		String suffix = imgFile.getName().substring(imgFile.getName().lastIndexOf(".") + 1);
		ByteArrayOutputStream b = new ByteArrayOutputStream();
		BufferedImage img = ImageIO.read(imgFile);
		int width = img.getWidth();
		int height = img.getHeight();
		if (w == null || w > width) {
			w = width;
		}
		if (h == null || h > height) {
			h = height;
		}
		BufferedImage bi = img.getSubimage(0, 0, w, h);
		ImageIO.write(bi, suffix, b);
		return b;
	}

	// 获取缩略图
	public static ByteArrayOutputStream thumbnailImage(File imgFile, Integer w, Integer h) throws IOException {
		String suffix = imgFile.getName().substring(imgFile.getName().lastIndexOf(".") + 1);
		ByteArrayOutputStream b = new ByteArrayOutputStream();
		BufferedImage img = ImageIO.read(imgFile);
		int width = img.getWidth();
		int height = img.getHeight();
		if (w == null) {
			ImageIO.write(img, suffix, b);
			return b;
		}
		if (h == null) {
			h = (int) ((double) height / width * w);
		}
		BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
		Graphics g = bi.getGraphics();
		g.drawImage(img, 0, 0, w, h, Color.LIGHT_GRAY, null);
		g.dispose();
		ImageIO.write(bi, suffix, b);
		return b;
	}

	public static String getUrlByFilePath(String filePath) {
		return filePath.replaceFirst("E:/res", RES_SERVER);
	}

	public static String getFilePathByUrl(String url) {
		return url.replaceFirst(RES_SERVER, "E:/res");
	}

	public static void deleteUrl(String url) {
		String filePath = FileUtil.getFilePathByUrl(url);
		File file = new File(filePath);
		file.delete();
	}

	public static void deleteUrls(List<String> urls) {
		File file;
		for (String url : urls) {
			String filePath = FileUtil.getFilePathByUrl(url);
			file = new File(filePath);
			file.delete();
		}
	}

}
